<?php
namespace ImiDemo\TcpDemo\MainServer\Listener;

use Imi\ConnectContext;
use Imi\Event\EventParam;
use Imi\Util\AtomicManager;
use Imi\Event\IEventListener;
use Imi\Bean\Annotation\Listener;

/**
 * @Listener("IMI.SERVER.GROUP.LEAVE")
 */
class GroupLeave implements IEventListener
{
    /**
     * 事件处理方法
     * @param EventParam $e
     * @return void
     */
    public function handle(EventParam $e)
    {
        $server = $e->getData()['server'];
        if('main' !== $server->getName())
        {
            return;
        }
        $people = $server->getGroup('g1')->count();
        $message = [
            'action'    =>    'send',
            'message'    =>    ConnectContext::get('username') . ' 离开了，在线人数：' . $people,
        ];
        $server->groupCall($e->getData()['groupName'], 'send', $server->getBean(\Imi\Server\DataParser\DataParser::class)->encode($message));
    }
}